package com.quanthium.service;

import com.quanthium.DeviceShadow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeviceShadowServiceImpl implements DeviceShadowService {

    // 回头调整到Dao层
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<DeviceShadow> findAll() {
        return mongoTemplate.findAll(DeviceShadow.class);
    }

    @Override
    public Optional<DeviceShadow> findById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        DeviceShadow deviceShadow = mongoTemplate.findOne(query, DeviceShadow.class);
        return Optional.ofNullable(deviceShadow);
    }

    @Override
    public DeviceShadow save(DeviceShadow deviceShadow) {
        return mongoTemplate.save(deviceShadow);
    }

    @Override
    public void deleteById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, DeviceShadow.class);
    }
}